package wx.controls
{
	import flash.display.DisplayObject;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import wx.core.Control;
	import wx.core.ITheme;
	import wx.core.SettingCache;

	/**
	 * 
	 * @author wujianqi
	 * 文本输入控件
	 * 
	 */	
	public class TextInput extends Control
	{
		private var _normalSkin:DisplayObject;
		private var _hoverSkin:DisplayObject;
		private var _focusInSkin:DisplayObject;
		private var _disabledSkin:DisplayObject;
		private var _focusOffset:Object;
		
		private var _text:String = "";		
		private var _displayAsPassword:Boolean = false;
		private var _restrict:String;
		private var _isMultiple:Boolean = false;
		
		private var _textInput:TextField;
		
		private var _currentState:String = "normal";
		private var _skinDisplay:DisplayObject;
		
		private const _theme:ITheme = SettingCache.theme;	
		
		public function TextInput()
		{
			super();			
			_normalSkin = _theme.inputNormalSkin;
			_focusInSkin = _theme.inputFocusInSkin;
			_hoverSkin = _theme.inputHoverSkin;
			_disabledSkin = _theme.inputDisableSkin;
			_focusOffset = _theme.inputFocusOffset;
			
			validSkinItem(_normalSkin);
			validSkinItem(_hoverSkin);
			validSkinItem(_focusInSkin);
			validSkinItem(_disabledSkin);
			
			addSkin();	
			addInput();
			
			if(_enable) bindEvent();			
		}

		public function set isMultiple(value:Boolean):void
		{
			if(_isMultiple!= value){
				_isMultiple = value;
				if(_textInput!=null) _textInput.wordWrap = value;
			}
		}

		public function set restrict(value:String):void
		{
			if(_restrict!= value){
				_restrict = value;
				if(_textInput!=null) _textInput.restrict = value;
			}
		}

		public function set displayAsPassword(value:Boolean):void
		{
			_displayAsPassword = value;
			if(_textInput!=null) _textInput.displayAsPassword = value;
		}
		
		
		/**
		 * 
		 * @param value
		 * 鼠标停留的皮肤
		 * 
		 */	
		public function set hoverSkin(value:DisplayObject):void
		{
			if(_hoverSkin!= value) _hoverSkin = value;
		}

		/**
		 * 
		 * @param value
		 * 禁用后的皮肤
		 * 
		 */		
		public function set disabledSkin(value:DisplayObject):void
		{
			if(_disabledSkin!= value) _disabledSkin = value;
		}

		/**
		 * 
		 * @param value
		 * 获取焦点后的皮肤
		 * 
		 */		
		public function set focusInSkin(value:DisplayObject):void
		{
			if(_focusInSkin!= value) _focusInSkin = value;
		}

		/**
		 * 
		 * @param value
		 * 正常皮肤
		 * 
		 */		
		public function set normalSkin(value:DisplayObject):void
		{
			if(_normalSkin!= value){
				_normalSkin = value;
				if(_currentState=="normal") updateDisplay();
			}
			
		}

		public function get text():String
		{
			return _textInput!=null?_textInput.text:_text;
		}

		public function set text(value:String):void
		{
			_text = value;
			if(_textInput!=null) _textInput.text = _text;
		}

		private function onHover(e:MouseEvent):void{
			if(_currentState != "hover"){
				_currentState = "hover";
				updateDisplay();
			}			
		}
		private function onMouseOut(e:MouseEvent):void{
			if(_currentState != "normal"){
				_currentState = "normal";
				updateDisplay();
			}
		}
		private function onDown(e:MouseEvent):void{
			e.stopPropagation();
		}		
		private function onFocusIn(e:FocusEvent):void{
			if(_currentState != "focusin"){
				_currentState = "focusin";
				updateDisplay();
			}
		}
		private function onFocusOut(e:FocusEvent):void{
			if(_currentState != "normal"){
				_currentState = "normal";
				updateDisplay();
			}
		}
		
		private function bindEvent():void{
			addEventListener(FocusEvent.FOCUS_IN,onFocusIn);
			addEventListener(FocusEvent.FOCUS_OUT,onFocusOut);
			addEventListener(MouseEvent.MOUSE_DOWN,onDown);
			addEventListener(MouseEvent.ROLL_OVER,onHover);
			addEventListener(MouseEvent.ROLL_OUT,onMouseOut);
		}
		private function unbindEvent():void{
			removeEventListener(FocusEvent.FOCUS_IN,onFocusIn);
			removeEventListener(FocusEvent.FOCUS_OUT,onFocusOut);
			removeEventListener(MouseEvent.MOUSE_DOWN,onDown);
			removeEventListener(MouseEvent.ROLL_OVER,onHover);
			removeEventListener(MouseEvent.ROLL_OUT,onMouseOut);
		}
		
		private function addSkin():void{			
			switch(_currentState)
			{
				case "normal":
				{
					_skinDisplay =_normalSkin;
					break;
				}
				case "focusin":
				{
					_skinDisplay =_focusInSkin;
					break;
				}
				case "hover":
				{
					_skinDisplay =_hoverSkin;
					break;
				}
				case "disabled":
				{
					_skinDisplay =_disabledSkin;
					break;
				}
			}			
			addChildAt(_skinDisplay,0);
		}
		
		private function addInput():void{
			_textInput = new TextField();
			_textInput.defaultTextFormat = new TextFormat(null, _theme.mediumFontSize, _theme.mediumFontColor);
			changeInputType(_enable);
			_textInput.text = _text;
			_textInput.displayAsPassword = _displayAsPassword;
			_textInput.restrict = _restrict;						
			_textInput.x = _focusOffset.left;
			_textInput.y = _focusOffset.top;
			
			if(_isMultiple){
				_textInput.multiline  = true;
				_textInput.wordWrap = true;
			}
			
			resize();
			addChildAt(_textInput,1);
			
			//var st:StageText = new StageText();
			//st.viewPort = new Rectangle(10,10,100,30);
			//st.stage = this.stage;
		}
		
		private function resize():void{
			_textInput.width = this.width -_focusOffset.left- _focusOffset.right;
			_textInput.height = this.height -_focusOffset.top- _focusOffset.bottom;
		}
		
		private function changeInputType(isInput:Boolean = true):void{
			if(_textInput!=null){
				if(isInput){
					_textInput.type = TextFieldType.INPUT;
					_textInput.selectable = true;
				}else{
					_textInput.type = TextFieldType.DYNAMIC;
					_textInput.selectable = false;
				}				
			}		
		}
		
		private function updateDisplay():void{
			if(_skinDisplay!=null && contains(_skinDisplay)) removeChild(_skinDisplay);
			addSkin();
		}
		
		
		override public function set enable(value:Boolean):void{
			if(_enable != value){
				_enable = value;
				_currentState = "disabled";
				if(!enable) unbindEvent();
				else bindEvent();
				changeInputType(_enable);
				updateDisplay();
			}					
		}
		
		
		override protected function dispose():void{
			unbindEvent();
		}
		
		override public function set width(value:Number):void{			
			super.width = value;
			resize();
		}
		override public function set height(value:Number):void{			
			super.height = value;
			resize();
		}
		
		
	}
}